package com.wf.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wf.domain.User;
import com.wf.domain.UserRole;
import com.wf.dto.UserDTO;
import com.wf.mapper.UserMapper;
import com.wf.mapper.UserRoleMapper;
import com.wf.service.UserRoleService;
import com.wf.service.UserService;
import com.wf.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Author: zhiyu
 * @Date:14:18
 * @DESC:
 */
@RestController
@Api("用户管理控制层")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private UserRoleService userRoleService;
    @ApiOperation("根据用户名查询用户，如果用户名为空则查询所有")
    @GetMapping("/selectUserByUsername")
    public Result selectUser(@RequestParam(value = "username",required = false) String username,@RequestParam(value = "role_id",required = false) Integer role_id){
        List<UserDTO> list = userService.selectUser(username, role_id);
        return Result.success(list);
    }
    @ApiOperation("逻辑删除用户信息")
    @DeleteMapping("/deleteUser")
    public Result deleteUser(@RequestParam("user_id") Integer user_id){
        boolean flag = false;
        int i = userMapper.deleteById(user_id);
        if (i>0){
            flag=true;
        }
        return Result.success(flag);
    }
    @Transactional
    @ApiOperation("批量逻辑删除用户信息")
    @DeleteMapping("/deleteUsers")
    public Result deleteUsers(@RequestBody List<Integer> users){
        System.out.println("users = " + users);
        int count = 0;
        boolean flag = false;
        for (int i = 0; i < users.size(); i++) {
            count += userMapper.deleteById(users.get(i));
        }
        if (count==users.size()){
            flag=true;
        }
        return Result.success(flag);
    }
    @ApiOperation("编辑用户状态")
    @PostMapping("/saveUser")
    @Transactional
    public Result updateUser(@RequestBody User user,@RequestParam(value ="roleId") String roleId){
        System.out.println("user = " + user);
        System.out.println("role_id = " + roleId);
        int i = userMapper.updateById(user);
        UserRole userRole = new UserRole();
        userRole.setRoleId(Long.parseLong(roleId));
        userRole.setUseId(user.getUserId());
        userRoleMapper.insert(userRole);
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("use_id",user.getUserId());
        boolean remove = userRoleService.remove(wrapper);
        int insert = userRoleMapper.insert(userRole);
        if (insert>0){
            return Result.success(true);
        }else {
            return Result.success(false);
        }
    }
}
